package nl.saxion.security.connection;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Class for setting up clientconnection
 * @author EII7SCb
 *
 */
public class ClientConnection extends Connection{

	private String ip; 

	public ClientConnection(int port, String ip, int timeOut)
	{
		super(port, timeOut);
		this.ip = ip;
	}
	
	/**
	 * Try to make connection with server
	 * @return True if connected
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws UnknownHostException 
	 */
	public boolean makeConnection() throws InterruptedException, IOException
	{
		int i = 0;
		while(i <= 10)
		{
			i++;
			try {
				connection = new Socket(ip, port);
			} catch (Exception e) {
				Thread.sleep(100);
			}
			
			if(connection != null)
			{
				break;
			}
		}
		
		if(i >= 10)
			return false;

		out = connection.getOutputStream();
		out.flush();
		in = connection.getInputStream();
	
		return true;
	}
	
}
